﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GetComics.Silverlight
{
    public class RotatableCanvas : ResizableCanvas
    {
        private Ellipse _rotateEllipse;
        private bool _mouseCaptured;


        public RotatableCanvas()
            : base()
        {
            this.MouseLeftButtonDown += new MouseButtonEventHandler(RotatableCanvas_MouseLeftButtonDown);

        }


        public FrameworkElement ElementToRotate
        {
            get;
            set;
        }

        private void RotatableCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (ElementToRotate != null)
                CreateRotateEllipse(ElementToRotate);
        }

        /// <summary>
        /// Добавляет к элементу эллипс
        /// </summary>
        /// <param name="element"></param>
        private void CreateRotateEllipse(FrameworkElement element)
        {
            if (_rotateEllipse == null)
            {
                _rotateEllipse = new Ellipse();
                _rotateEllipse.Height = 20;
                _rotateEllipse.Width = 20;
                _rotateEllipse.Stroke = new SolidColorBrush(Colors.Black);
                _rotateEllipse.StrokeThickness = 3;
                _rotateEllipse.Fill = new SolidColorBrush(Colors.Red);
                Canvas.SetTop(_rotateEllipse, element.Height / 2 + 100);
                Canvas.SetLeft(_rotateEllipse, element.Width / 2 - 10);
                _rotateEllipse.MouseLeftButtonDown += new MouseButtonEventHandler(rotateEllipse_MouseLeftButtonDown);
                _rotateEllipse.MouseLeftButtonUp += new MouseButtonEventHandler(rotateEllipse_MouseLeftButtonUp);
                _rotateEllipse.MouseMove += new MouseEventHandler(rotateEllipse_MouseMove);

                this.Children.Add(_rotateEllipse);
                Canvas.SetZIndex(_rotateEllipse, Canvas.GetZIndex(element) - 1);
            }
        }

        void rotateEllipse_MouseMove(object sender, MouseEventArgs e)
        {
            if (_mouseCaptured)
            {
                RotateTransform rtTrans = ((TransformGroup)ElementToRotate.RenderTransform).GetTransform<RotateTransform>();
                TranslateTransform trTrans = ((TransformGroup)ElementToRotate.RenderTransform).GetTransform<TranslateTransform>();

                // Вычисляем разницы между центром облака и положением красной хрени
                double deltaX = e.GetPosition(ElementToRotate as SuperCanvas).X - Canvas.GetLeft(ElementToRotate) - (ElementToRotate.Height / 2) - trTrans.X;
                double deltaY = e.GetPosition(ElementToRotate as SuperCanvas).Y - Canvas.GetTop(ElementToRotate) - (ElementToRotate.Width / 2) - trTrans.Y;

                rtTrans.CenterX = ElementToRotate.Width / 2;
                rtTrans.CenterY = ElementToRotate.Height / 2;
                rtTrans.Angle = 180 * (-Math.Atan(deltaX / deltaY)) / Math.PI;

                if (deltaY < 0) rtTrans.Angle += 180;
            }
        }

        void rotateEllipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Ellipse ellipse = (Ellipse)sender;
            ellipse.ReleaseMouseCapture();
            _mouseCaptured = false;
            e.Handled = true;
        }

        void rotateEllipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Ellipse ellipse = (Ellipse)sender;
            ellipse.CaptureMouse();
            _mouseCaptured = true;
            e.Handled = true;
        }

    }
}
